<?php
declare (strict_types=1);

namespace app\common\model;

class Area extends Base
{
    protected $name = 'area';


    public static function getArea($pid = 0, $deep = 0)
    {
        return parent::where(['pid' => $pid, 'deep' => $deep])
            ->field('id,ext_name as title')->select();
    }

    public static function mate($deep = 0, $p = '', $c = '', $d = '', $t = ''): string
    {
        try {
            $address = '';
            switch ($deep) {
                case 4:
                    $area = [$p];

                    break;
                case 3:
                    $area = [$p, $c];
                    break;
                case 2:
                    $area = [$p, $c, $d];
                    break;
                case 1:
                    $area = [$p, $c, $d, $t];
                    break;
                default:
                    $area = [];
            }
            foreach ($area as $item) {
                if ($item) {
                    $address .= parent::where('id', $item)->value('ext_name');
                }
            }
            return $address;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }
}
